package ai.leinao.alarm.notify;

import ai.leinao.notify.core.enums.NotifyType;
import ai.leinao.notify.core.template.AbstractTemplateRegistry;
import ai.leinao.notify.core.template.TemplateProperties;
import ai.leinao.notify.core.template.TemplateProvider;
import ai.leinao.notify.dingtalk.enums.DingTalkProviderEnum;
import ai.leinao.notify.email.EmailProviderEnum;
import ai.leinao.notify.webhook.WebHookProviderEnum;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 基于数据库的模板注册
 *
 * @author zhong.wang
 * @date 2025/9/9
 **/
@Component
public class DBTemplateRegistry extends AbstractTemplateRegistry implements BeanPostProcessor {
    @Override
    protected TemplateProperties getProperties(NotifyType notifyType, String templateId) {
        if(notifyType == NotifyType.EMAIL && templateId.equals("dbAlarmEmail")){
            TemplateProperties templateProperties = new TemplateProperties();
            templateProperties.setId("dbAlarmEmail");
            templateProperties.setName("dbAlarmEmail");
            templateProperties.setNotifyType(NotifyType.EMAIL);
            templateProperties.setProvider(EmailProviderEnum.EMAIL.getType());
            templateProperties.setDescription("email");
            Map<String, Object> content = new HashMap<>();
            content.put("subject", "告警通知");
            content.put("text","${groupKey}出现了${alarms}");
            content.put("sendTo", "${sendTo}");
            templateProperties.setTemplate(content);
            return templateProperties;
        }else if(notifyType == NotifyType.HTTP && templateId.equals("dbAlarmHttp")){
            TemplateProperties templateProperties = new TemplateProperties();
            templateProperties.setId("dbAlarmHttp");
            templateProperties.setName("dbAlarmHttp");
            templateProperties.setNotifyType(NotifyType.HTTP);
            templateProperties.setProvider(WebHookProviderEnum.WEBHOOK.getType());
            templateProperties.setDescription("http");
            Map<String, Object> content = new HashMap<>();
            content.put("body", "{\"time\": \"${time}\",\"alarmContent\": \"${alarmContent}\",\"stationName\": \"${stationName}\",\"deviceName\": \"${deviceName}\"}");
            templateProperties.setTemplate(content);
            return templateProperties;
        }else if(notifyType == NotifyType.DING_TALK && templateId.equals("dbAlarmDingTalk")){
            TemplateProperties templateProperties = new TemplateProperties();
            templateProperties.setId("dbAlarmDingTalk");
            templateProperties.setName("dbAlarmDingTalk");
            templateProperties.setNotifyType(NotifyType.DING_TALK);
            templateProperties.setProvider(DingTalkProviderEnum.DING_TALK_ROBOT.getType());
            templateProperties.setDescription("dingTalk");
            Map<String, Object> content = new HashMap<>();
            content.put("title", "设备告警");
            content.put("messageType", "markdown");
            content.put("text", "${time}${stationName}/${deviceName}出现了${alarmContent}");
            templateProperties.setTemplate(content);
            return templateProperties;
        }

        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof TemplateProvider) {
            this.registerProvider((TemplateProvider) bean);
        }
        return bean;
    }
}
